//
//  MyPageController.swift
//  AAPageController
//
//  Created by Aaron on 2019/4/16.
//  Copyright © 2019 Aaron. All rights reserved.
//

import UIKit
import AAPageController

class MyPageController: AAPageController {
    
    var ctrs: [UIViewController] = []
    var titles:[String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        for _ in 0..<10 {
            ctrs += [FirstTableViewController(), SecondTableViewController()]
            titles += ["哈哈哈", "呵呵呵呵"]
        }
        self.selectedColor = .purple
        self.dataSource = self
        self.delegate = self
        self.topBarItemWidth = 90
        self.topBarItemSpace = 10
        self.topBarHeight = 50
        self.isCycling = false
        
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            self.scrollToIndex = 2
            self.reloadData()
//        }
    }
    
    /*
     // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MyPageController: AAPageControllerDataSource, AAPageControllerDelegate {
    
    func indexOfChildController(pageController: AAPageController, child: UIViewController) -> Int {
        return ctrs.firstIndex(of: child)!
    }
    
    func numbersOfChildControllers(pageController: AAPageController) -> Int {
        return ctrs.count
    }
    
    func childControllers(pageController: AAPageController, index: Int) -> UIViewController {
        return ctrs[index]
    }
    
    func titlesForChildControllers(pageController: AAPageController, index: Int) -> String {
        return "\(titles[index]) \(index)"
    }
    
    func pageController(_: AAPageController, didDisplayedChildAt index: Int) {
        print("display at: \(index)")
    }
    
    func iconsForChildControllers(pageController: AAPageController, index: Int) -> IconInfo? {
        return (Icon.url(url: "https://lanhu.oss-cn-beijing.aliyuncs.com/SketchPng5e5e3e79d81faeb10567a4e2c423b8e9d025396b09bc08d5770f93ec1167e8b9"), CGSize.init(width: 18, height: 18))
    }
    
}
